<?php

namespace App\Services;

use App\Models\SysRole;
use Illuminate\Support\Facades\DB;

/**
 * 角色服务类
 * @package App\Services
 */
class SysRoleService
{
    /**
     * 获取角色列表
     * @return mixed
     */
    public function getRoleList()
    {
        $roleList = SysRole::orderBy('order_num','ASC')->orderBy('role_id','ASC')->get();
        $newList = [];
        foreach ($roleList as $k => $role){
            $role['permIds'] = $role->permissions()->get()->pluck('perm_id')->toArray();
            $newList[$k] = $role->toArray();
        }
        return $newList;

    }

    /**
     * 保存角色数据
     * @param $request
     * @return bool
     */
    public function saveData($request)
    {
        $roleId = $request->input('role_id', 0);
        if ($roleId){
            $roleModel = SysRole::find($roleId);
        }else{
            $roleModel = new SysRole;
        }
        $roleModel->parent_id = $request->input('parent_id', 0);
        $roleModel->name = $request->input('name', '');
        $roleModel->display_name = $request->input('display_name', '');
        $roleModel->description = (string)$request->input('description', '');
        $roleModel->order_num = (int) $request->input('order_num', 0);

        return $roleModel->save();
    }

    /**
     * 获取角色数据
     * @param $roleId
     * @return mixed
     */
    public function get($roleId)
    {
        $roleData = SysRole::findOrFail($roleId);
        return $roleData;
    }

    /**
     * 保存菜单排序
     * @param $sortData
     * @return bool
     * @throws \Exception
     */
    public function saveSort($sortData)
    {
        Db::beginTransaction();
        try{
            $this->saveSortData($sortData);
            Db::commit();
            return true;
        }catch (\Exception $e){
            Db::rollBack();
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 递归保存菜单排序数据
     * @param $list
     * @param int $parentId
     * @return bool
     */
    private function saveSortData($list, $parentId = 0)
    {
        if ($list) {
            foreach ($list as $k => $v) {
                $id = isset($v['id']) && $v['id'] ? (int)$v['id'] : 0;
                if ($id){
                    Db::table('sys_roles')->where('role_id', $id)->update(['order_num'=>$k, 'parent_id' => $parentId]);
                }
                if (isset($v['children']) && $v['children']) {
                    $this->saveSortData($v['children'], $id);
                }
            }
            return true;
        }
    }

    /**
     * 保存角色权限
     * @param $roleId
     * @param $permIds
     * @return mixed
     */
    public function savePerm($roleId, $permIds)
    {
        $model = SysRole::findOrFail($roleId);
        if (empty($permIds)){
            throw new \Exception('请选择授权给角色的权限');
        }
        return $model->permissions()->sync($permIds);
    }
    /**
     * 删除角色
     * @param $roleIds
     * @return int
     */
    public function del($roleIds)
    {
        return SysRole::destroy($roleIds);
    }
}
